package com.such.kit.database.bean;

import java.util.ArrayList;
import java.util.List;
import com.such.kit.validate.ValidateCommons;

public class SqlPackage {

	private String sql;
	private List<Object> args;

	public SqlPackage(String sql) {
		this.sql = sql;
	}

	public SqlPackage(String sql, List<Object> args) {
		this.sql = sql;
		this.args = args;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public String getSql() {
		return this.sql;
	}

	public void setArgs(List<Object> args) {
		this.args = args;
	}

	public List<Object> getArgs() {
		return this.args;
	}

	@Override
	public String toString() {
		if (ValidateCommons.isEmpty(this.sql)) {
			return "";
		} else if (ValidateCommons.isEmpty(this.args)) {
			return this.sql;
		} else {
			String tempSql = this.sql;
			List<Object> tempArgs = new ArrayList<Object>(this.args);
			while(this.sql.indexOf("?") != -1 && tempArgs.size() > 0) {
				tempSql = tempSql.replaceFirst("\\?", tempArgs.get(0).toString());
				tempArgs.remove(0);
			}
			return tempSql.replaceAll("(\r?\n(\\s*\r?\n)+)", "\r\n");
		}
	}

}
